/*
 * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
 *
 * Licensed under the Aduna BSD-style license.
 */
 
package edu.tju.commons.text;


/**
 * Utility methods for regular expression patterns.
 *
 * @see java.lang.regex.Pattern
 */
public class PatternUtil {

	/**
	 * Escapes any characters that have a special meaning in Patterns.
	 */
	public static String escapeSpecialChars(String s) {
		s = StringUtil.gsub("\\", "\\\\", s);
		s = StringUtil.gsub("[", "\\[", s);
		s = StringUtil.gsub("]", "\\]", s);
		s = StringUtil.gsub("(", "\\(", s);
		s = StringUtil.gsub(")", "\\)", s);
		s = StringUtil.gsub(".", "\\.", s);
		s = StringUtil.gsub("?", "\\?", s);
		s = StringUtil.gsub("*", "\\*", s);
		s = StringUtil.gsub("+", "\\+", s);
		s = StringUtil.gsub("^", "\\^", s);
		s = StringUtil.gsub("$", "\\$", s);
		return s;
	}
}
